package com.googlecode.horncomb.web.spring.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

public class CommandOutputStreamResponeController extends
		BaseSimpleFormController implements InitializingBean {
	private String defaultContentType = "application/x-download";
	String filenameEncoding = "GBK";
	private String defaultFileName = "noname";
	private int readerBufferSiz = 4096;
	/** 是否是附件下载方式 */
	private boolean attachment = true;

	private CommandServletResponeInputStreamProvider inputStreamProvider = null;

	private ServletResponseStreamDelegate delegate = new OutputStreamResponeController();

	public void afterPropertiesSet() throws Exception {
		Assert.notNull(this.inputStreamProvider,
				"the property responseStreamProvider must be not null");
		this.delegate.setDefaultContentType(this.defaultContentType);
		this.delegate.setDefaultFileName(this.defaultFileName);
		this.delegate.setReaderBufferSiz(this.readerBufferSiz);
		this.delegate.setAttachment(this.attachment);
		this.delegate.setFilenameEncoding(this.filenameEncoding);
	}

	@Override
	protected boolean isFormSubmission(HttpServletRequest request) {
		return true;
	}

	@Override
	protected boolean isFormChangeRequest(HttpServletRequest request,
			Object command) {
		return false;
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ServletResponeInputStreamSource fsr = this.inputStreamProvider
				.getInputStreamSource(request, command);
		this.delegate.createResponse(request, response, fsr);
		return null;
	}

	/**
	 * @param defaultContentType
	 *            the defaultContentType to set
	 */
	public void setDefaultContentType(String defaultContentType) {
		Assert.hasText(defaultContentType);
		this.defaultContentType = defaultContentType;
	}

	/**
	 * @param readerBufferSiz
	 *            the readerBufferSiz to set
	 */
	public void setReaderBufferSiz(int readerBufferSiz) {
		Assert.isTrue(readerBufferSiz > 0);
		this.readerBufferSiz = readerBufferSiz;
	}

	/**
	 * @param defaultFileName
	 *            the defaultFileName to set
	 */
	public void setDefaultFileName(String defaultFileName) {
		this.defaultFileName = defaultFileName;
	}

	/**
	 * @param servletResponeInputStreamProvider
	 *            the servletResponeInputStreamProvider to set
	 */
	public void setInputStreamProvider(
			CommandServletResponeInputStreamProvider inputStreamProvider) {
		this.inputStreamProvider = inputStreamProvider;
	}

	/**
	 * @param attachment
	 *            the attachment to set
	 */
	public void setAttachment(boolean attachment) {
		this.attachment = attachment;
	}

	/**
	 * @param filenameEncoding
	 *            the filenameEncoding to set
	 */
	public void setFilenameEncoding(String filenameEncoding) {
		this.filenameEncoding = filenameEncoding;
	}
}
